package sany.iec.service.ems;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import sany.iec.common.GlobalConstant;
import sany.iec.config.IecEmsServer;
import sany.iec.entity.IECData;
import sany.iec.service.IecServerService;
import sany.iec.service.IecSendMessage;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author : 托马斯
 * @Date : 2025/7/2 12:55
 * @Description :
 */
@Slf4j
@Service
public class EMSSchedule {

    @Resource(name = "EMS")
    private IecServerService iecServerService;

    @Resource
    private EMSDataService emsDataService;

    @Resource
    private IecSendMessage iecSendMessage;

    @Resource
    private IecEmsServer iecEmsServer;

    @Scheduled(initialDelay = 5000, fixedRate = 2000)
    public void insertData() {
        Map<Integer, Float> map =  GlobalConstant.RECEIVE_DATA.getOrDefault(iecEmsServer.getPort(), new HashMap<>());
        if (map.containsKey(25089)) {
            // 说明存在有功 agcValue 指令
            Float v = map.get(25089);
            if (!emsDataService.updateOrigin("PowerSum_RT", v)) {
                log.warn("修改失败~~~   key: {}", "PowerSum_RT");
            }
            if (!emsDataService.updateOrigin("PowerTheoryS_RT", emsDataService.getInstallSum().floatValue() - 1)) {
                log.warn("修改失败~~~   key: {}", "PowerTheoryS_RT");
            }
            if (!emsDataService.updateOrigin("PowerAvailableUpperLimitS_RT", emsDataService.getInstallSum().floatValue() - 2)) {
                log.warn("修改失败~~~   key: {}", "PowerAvailableUpperLimitS_RT");
            }
            if (!emsDataService.updateOrigin("FrmPowerTheoryAW_RT", emsDataService.getInstallSum().floatValue() - 1)) {
                log.warn("修改失败~~~   key: {}", "FrmPowerTheoryAW_RT");
            }
            if (!emsDataService.updateOrigin("PowerAvailableUpperLimitAW_RT", emsDataService.getInstallSum().floatValue() - 2)) {
                log.warn("修改失败~~~   key: {}", "PowerAvailableUpperLimitAW_RT");
            }
            if (!emsDataService.updateOrigin("PowerLimit_FB", v)) {
                log.warn("修改失败~~~   key: {}", "PowerLimit_FB");
            }
            if (!emsDataService.updateOrigin("RePowerTheoryUpperLimit", emsDataService.getInstallSum().floatValue() * 0.328f)) {
                log.warn("修改失败~~~   key: {}", "RePowerTheoryUpperLimit");
            }
            if (!emsDataService.updateOrigin("RePowerTheoryLowerLimit", emsDataService.getInstallSum().floatValue() * 0.328f * -1)) {
                log.warn("修改失败~~~   key: {}", "RePowerTheoryLowerLimit");
            }
        }
        if (map.containsKey(25090)) {
            // 说明存在有功 avcValue 指令
            Float avc = map.get(25090);
            if (!emsDataService.updateOrigin("RePowerSum_RT", avc * 0.999f)) {
                log.warn("修改失败~~~   key: {}", "RePowerSum_RT");
            }
            if (!emsDataService.updateOrigin("RePowerLimit_FB", avc)) {
                log.warn("修改失败~~~   key: {}", "RePowerLimit_FB");
            }
        }
    }

    @Scheduled(initialDelay = 5000, fixedRate = 2000)
    public void schedule() {
        for (IECData iecDatum : emsDataService.getIEC_DATA()) {
            if (iecDatum.getType().equals("yc")) {
                iecSendMessage.broadcastAnalogValues(iecServerService, updateIECData(iecDatum));
            } else if (iecDatum.getType().equals("yx")) {
                iecSendMessage.broadcastStatusValues(iecServerService, updateIECData(iecDatum));
            } else {
                log.warn("不属于遥信遥测，不执行指令发送。Data: {}", iecDatum);
            }
        }
        log.info("EMS 装机容量: {}  实发有功功率: {}  实发无功功率；{}", emsDataService.getKey("InstalledCapacity"),
                emsDataService.getKey("PowerSum_RT"), emsDataService.getKey("RePowerSum_RT"));
    }

    /**
     * 修改数据
     * @param iecDatum : 数据
     * @return
     */
    private IECData updateIECData(IECData iecDatum) {
        if (iecDatum.getType().equals("yc")) {
            Double b = ((Math.random() * 2) - 1) * iecDatum.getChazhi();
            iecDatum.setValue(b + Float.parseFloat(iecDatum.getOriginValue().toString()));
        } else if (iecDatum.getType().equals("yx")) {
            iecDatum.setValue(Float.parseFloat(iecDatum.getOriginValue().toString()) == 1.0f ? 1 : 0);
        }
        return iecDatum;
    }


}
